package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2020-06-16
 */
//@CrossOrigin
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll(){

        //log.info("所有讲师列表");
        //log.warn("所有讲师列表");
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    @ApiOperation(value = "根据id删除讲师", notes = "根据id删除讲师，逻辑删除" )
    @DeleteMapping("remove/{id}")
    public R removeById(
            @ApiParam(value = "讲师id", required = true)
            @PathVariable String id){

        //删除图片
        teacherService.removeAvatarById(id);

        //删除讲师
        boolean b = teacherService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)@PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)@PathVariable Long limit,
            @ApiParam(value = "讲师查询对象") TeacherQuery teacherQuery){


        Page<Teacher> pageParam = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageModel", pageParam);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(
            @ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){

        boolean r = teacherService.save(teacher);
        if(r){
            return R.ok().message("添加成功");
        }else{
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("修改讲师")
    @PutMapping("update")
    public R updateById(
            @ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){

        boolean r = teacherService.updateById(teacher);
        if(r){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师id", required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if(teacher != null){
            return R.ok().data("item", teacher);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "讲师id列表", required = true)
            @RequestBody List<String> idList){
        boolean result = teacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据左关键字查询讲师名列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字", required = true)
            @PathVariable String key){

        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }

    @ApiOperation("测试微服务远程调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }
}

